from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash

db = SQLAlchemy()

class User(db.Model):
  __tablename__ = 'users'
  uid = db.Column(db.Integer, primary_key = True)
  username = db.Column(db.String(100))
  email = db.Column(db.String(120), unique=True)
  pwdhash = db.Column(db.String(54))
  tweetsum = db.Column(db.Integer)
  credit = db.Column(db.Integer)
   
  def __init__(self, username, email, password, tweetsum=0, credit =0):
    self.username = username.title()
    self.email = email.lower()
    self.set_password(password)
    self.tweetsum = tweetsum
    self.credit = credit
     
  def set_password(self, password):
    self.pwdhash = generate_password_hash(password)
   
  def check_password(self, password):
    return check_password_hash(self.pwdhash, password)